package com.central.apps.controller;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.annotation.log.BussinessLog;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "资产模板接口")
@RequestMapping("/assetTemp")
public class AssetTempController extends BaseHandler {

    @Autowired private HandlerService handlerService;

    @PostMapping("/syncInfos")
    @BussinessLog(module = "apps-center", value = "获取资产模板选择的资产编号,将模板信息更新到对应资产台账数据")
    public void syncInfos (@RequestParam String assetnum, @RequestParam String tempnum){
        //根据tempnum模板编号获取资产模板信息,更新到台账表
        String getInfosSql ="select * from bl_assetTemplate where templatenum ='"+tempnum+"';";
        List<Map<String, Object>> getInfosSqlLists = handlerService.findBySql(getInfosSql);
        String equipmentclassify =(String) getInfosSqlLists.get(0).get("equipmentclassify");
        String classifydescribe =(String) getInfosSqlLists.get(0).get("classifydescribe");
        String classification =(String) getInfosSqlLists.get(0).get("classification");
        String manufacturer =(String) getInfosSqlLists.get(0).get("manufacturer");
        String specification =(String) getInfosSqlLists.get(0).get("specification");
        //更新
        String updSql ="update bl_asset set equipmentclassify='"+equipmentclassify+"',"+
                                            "classifydescribe='"+classifydescribe+"',"+
                                            "classification='"+classification+"',"+
                                            "manufacturer='"+manufacturer+"',"+
                                            "specification='"+specification+"' where assetnum='"+assetnum+"';";
        handlerService.updateBySql(updSql);
        //更新备件表
        //根据资产编号获取备件信息
        String bjsql ="select * from bl_replacementpart where tempnum ='"+tempnum+"';";
        List<Map<String, Object>> bySql = handlerService.findBySql(bjsql);
        for (int i =0;i<bySql.size();i++){
            int id = (int) bySql.get(0).get("id");
            String updSql2 ="update bl_replacementpart set assetnum ='"+assetnum+"' where id ="+id+";";
            handlerService.updateBySql(updSql2);
        }
        //更新技术参数表
        String bjsql2 ="select * from bl_technicalspecification where tempnum ='"+tempnum+"';";
        List<Map<String, Object>> bySql2 = handlerService.findBySql(bjsql2);
        for (int i =0;i<bySql2.size();i++){
            int id = (int) bySql2.get(0).get("id");
            String updSql3 ="update bl_technicalspecification set assetnum ='"+assetnum+"' where id ="+id+";";
            handlerService.updateBySql(updSql3);
        }
    }
}
